package lib.servlet;

import lib.Dao.Dbutil;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 回调地址
 *
 * @author wlw
 *
 */

@WebServlet("/CallBackOrder")
public class CallBackToOrder extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 3.获取code
         */
        String code=req.getParameter("code");
        String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+AuthUtil.APP_ID
                + "&secret="+AuthUtil.APP_SECRET
                + "&code="+code
                + "&grant_type=authorization_code";
        JSONObject jsonObject;
        try {
            jsonObject = AuthUtil.doGetJson(url);
            String openid=jsonObject.getString("openid");
            String token=jsonObject.getString("access_token");
            /**
             * 4.拉取用户信息
             */
            String infoUrl="https://api.weixin.qq.com/sns/userinfo?access_token="+token
                    + "&openid="+openid
                    + "&lang=zh_CN";
            JSONObject userInfo=AuthUtil.doGetJson(infoUrl);

            req.getSession().setAttribute("openid",openid);
            //使用微信用户信息直接登录，无需注册和绑定
            System.out.println(openid);
            req.getSession().setAttribute("info", userInfo);

            try{
                Statement stmt = null;
                Dbutil dbutil = new Dbutil();
                Connection con = null;
                ResultSet rs = null;
                con = dbutil.getCon();
                PreparedStatement pst = null;
                //System.out.println(content);
                stmt = con.createStatement();
                // INSERT INTO Student(sId,sName) VALUES (?,?)  "delete from notes where notes_id = ?";


                String sql2= "select * from user where openid like '"+openid+"'";
                rs = stmt.executeQuery(sql2);
                if(!rs.next()){
                    String sql1 = "insert into user (openid,count) values ("+"'"+openid+"'"+",0)";
                    stmt.executeUpdate(sql1);
                    resp.sendRedirect("/presetschool");
                    return;
                }

            }catch (Exception e) {
                e.printStackTrace();
            }
            resp.sendRedirect("/order");
            //req.getRequestDispatcher("/index1.jsp").forward(req, resp);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doPost(req, resp);
    }

}